﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DigitalSolutionLibrary.SqlDatabase;
using System.Threading;

namespace DigitalSolutionManagement
{
    public partial class frmListEmployee : BaseFormList
    {
        /*
        private Thread th;
        private delegate void UpdateUI(DataSet ds, int totalPage);
         */
        public frmListEmployee()
        {
            InitializeComponent();

            LoadDataStore = "GetEmployeeList";
            EditForm = new frmEditEmployee();
            DeleteItemStore = "DeleteEmployee";
            RptTye = frmViewReport.ReportType.VIEW_EMP;
        }

        /*
        private void LoadEmployeeList(int page)
        {
            Parameter par = new Parameter("CurPage", page, SqlDbType.Int);
            ParameterStock stock = new ParameterStock();
            stock.AddParamter(par);

            par = new Parameter("RowPerPage", Program.RowPerPage, SqlDbType.Int);
            stock.AddParamter(par);

            par = new Parameter("TotalPage", null, SqlDbType.Int, ParameterDirection.Output);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore("GetEmployeeList", stock, DatabaseWrapper.ReturnType.DATASET);
            if (obj != null)
            {
                DataSet ds = (DataSet)obj;
                int totalPage = (int)stock.GetAtPosition(2).ValueOut;
                UpdateUI uui = new UpdateUI(UpdateControl);
                Invoke(uui, new object[] { ds, totalPage });
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kết nối đến server, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UpdateControl(DataSet ds, int totalPage)
        {
            dataGridView1.DataSource = ds.Tables[0];
            paging1.TotalPage = totalPage;
            listPanel1.EnableDelete = listPanel1.EnableEdit = listPanel1.EnableView = dataGridView1.SelectedRows.Count > 0;
            processing1.HideProcessing();
        }

        private void DeleteAndLoadEmpList(int empId)
        {
            Parameter par = new Parameter("EmpId", empId, SqlDbType.Int);
            ParameterStock stock = new ParameterStock();
            stock.AddParamter(par);

            par = new Parameter("RowPerPage", Program.RowPerPage, SqlDbType.Int);
            stock.AddParamter(par);
            object obj = Program.dbWrapper.ExecuteStore("DeleteEmployee", stock, DatabaseWrapper.ReturnType.FIRST_CELL);
            if (obj != null)
            {
                int tp = (int)obj;
                if (paging1.CurrentPage > tp)
                {
                    paging1.CurrentPage = tp;
                    paging1.TotalPage = tp;
                    LoadEmployeeList(tp);
                }
                else
                {
                    LoadEmployeeList(paging1.CurrentPage);
                }
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kết nối đến server, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void listPanel1_AddClick(object sender, EventArgs e)
        {
            frmEditEmployee frm = new frmEditEmployee(0, BaseFormEdit.OpenMode.NEW);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                paging1.CurrentPage = 1;
                processing1.ShowProcessing();
                th = new Thread(() => LoadEmployeeList(1));
                th.Start();
            }
        }

        private void listPanel1_DeleteClick(object sender, EventArgs e)
        {
            string empName = dataGridView1.SelectedRows[0].Cells["ColName"].Value.ToString();
            DialogResult drs = MessageBox.Show("Bạn có chắc muốn xóa nhân viên \"" + empName + "\"?", "DSCo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (drs == System.Windows.Forms.DialogResult.Yes)
            {
                int empId = (int)dataGridView1.SelectedRows[0].Cells["ColEmpId"].Value;
                processing1.ShowProcessing();
                th = new Thread(() => DeleteAndLoadEmpList(empId));
                th.Start();
            }
        }

        private void listPanel1_EditClick(object sender, EventArgs e)
        {
            int empId = (int)dataGridView1.SelectedRows[0].Cells["ColEmpId"].Value;
            frmEditEmployee frm = new frmEditEmployee(empId, BaseFormEdit.OpenMode.EDIT);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                processing1.ShowProcessing();
                th = new Thread(() => LoadEmployeeList(paging1.CurrentPage));
                th.Start();
            }
        }

        private void listPanel1_ViewClick(object sender, EventArgs e)
        {
            int empId = (int)dataGridView1.SelectedRows[0].Cells["ColEmpId"].Value;
            frmViewReport frmViewRpt = new frmViewReport(frmViewReport.ReportType.VIEW_EMP);
            frmViewRpt.ItemId = empId;
            frmViewRpt.Show();
        }

        private void frmListEmployee_Load(object sender, EventArgs e)
        {
            processing1.ShowProcessing();
            th = new Thread(() => LoadEmployeeList(1));
            th.Start();
        }

        private void paging1_PageChanged(object sender, EventArgs e)
        {
            processing1.ShowProcessing();
            th = new Thread(() => LoadEmployeeList((int)sender));
            th.Start();
        }
         */
    }
}
